package com.gyg.service.impl;

import com.gyg.domain.CloudPicture;
import com.gyg.dao.CloudPictureMapper;
import com.gyg.returns.RespCode;
import com.gyg.returns.RespEntity;
import com.gyg.returns.RespTable;
import com.gyg.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gyg.vo.PageVO;
import com.gyg.vo.param.*;
import com.gyg.vo.result.PictureResult;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 图片信息表 服务实现类
 * </p>
 *
 * @author gyg
 * @since 2021-04-14
 */
@Service
public class CloudPictureServiceImpl extends ServiceImpl<CloudPictureMapper, CloudPicture> implements CloudPictureService {

    @Override
    public RespTable<List<PictureResult>> pictureList(FileParam fileParam) {

        Integer count = baseMapper.pictureCount(fileParam);

        if (Objects.isNull(count) || 0 == count) {
            return new RespTable<>(RespCode.OK, new ArrayList<>(), 0);
        }

        PageVO pageVO = new PageVO(fileParam.getPage(), fileParam.getLimit(), count);
        fileParam.setPage(pageVO.getBegin() - 1);
        List<PictureResult> pictureResultList = baseMapper.pictureList(fileParam);

        return new RespTable<>(RespCode.OK, pictureResultList, count);
    }

    @Override
    public RespEntity banPicture(BanParam banParam) {
        CloudPicture cloudPicture = CloudPicture.builder()
                .id(banParam.getFileId())
                .pictureBan(1)
                .build();
        baseMapper.updateById(cloudPicture);

        return new RespEntity(RespCode.SUCCESS);
    }

    @Override
    public RespEntity unsealPicture(BanParam banParam) {
        CloudPicture cloudPicture = CloudPicture.builder()
                .id(banParam.getFileId())
                .pictureBan(0)
                .build();
        baseMapper.updateById(cloudPicture);

        return new RespEntity(RespCode.SUCCESS);
    }

}
